<?php

class util {


	public static function getBrowser(){
        // Declare known browsers to look for 
        $known = array( 'msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko', 'chrome' ); 
        
        /* 
         * Clean up agent and build regex that matches phrases for known browsers 
         * (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor version numbers.  
         * E.g. "2.0.0.6" is parsed as simply "2.0" 
         */ 
        $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] ); 
        $pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#'; 
    
        // Find all phrases (or return empty false if none found) 
        $matches = array(); 
        if( !preg_match_all( $pattern, $agent, $matches ) ) { 
            return false; 
        } 
        
        // determine which index to use 
        if( count( $matches['browser'] ) > 1 ) { 
            $i = 1; 
        } 
        else { 
            $i = 0; 
        } 
        
        // return the results 
        return array( $matches['browser'][$i] => $matches['version'][$i] ); 
   	}
   	
   	public static function timer(){
	    list ($msec, $sec) = explode(' ', microtime()); 
	    $microtime = (float)$msec + (float)$sec; 
	    return $microtime; 
	}

	public static function flatten($array, $flat = false) { 
		if (!is_array($array) || empty($array)) return ''; 
		if (empty($flat)) $flat = array(); 
	    
		foreach ($array as $key => $val) { 
			if (is_array($val)) $flat = self::flatten($val, $flat); 
			else $flat[] = $val; 
		} 	    
		return $flat; 
	} 
	
	public static function urltopath($url){
		return str_replace(SYS_BASEURL, SYS_BASEDIR, $url);
	}
	
	public static function pathtourl($url){
		return str_replace(SYS_BASEDIR, SYS_BASEURL, $url);
	}
	
	final public static function filenameNoExt($val){
		preg_match("/([^\/]+)\.[\w]{2,}$/", $val, $matches);
		
		if(count($matches) > 1){
			return $matches[1];
		} else {
			return $val;
		}
	}

} 

?>